package com.zb.spring5.ioc.entity;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import java.security.MessageDigest;

/**
 * 使用bean工厂  来生产实例
 * 不实现FactoryBean接口的话,需要在配置文件里指明工厂类 ,工厂方法
 * factory-bean
 * factory-method
 */

public class MessageDigestFactoryBean implements FactoryBean<MessageDigest>, InitializingBean {


    private String algorithmName = "MD5";
    private MessageDigest messageDigest;
    @Override
    public MessageDigest getObject() throws Exception {
        return messageDigest;
    }

    @Override
    public Class<?> getObjectType() {
        return MessageDigest.class;
    }
    public boolean isSingleton(){
        return true;
    }

    /**
     * 初始化回调方法 实现接口InitializingBean，可以不用在配置里配置初始化方法
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        messageDigest = MessageDigest.getInstance(algorithmName);
    }
    public void setAlgorithmName(String algorithmName) {
        this.algorithmName = algorithmName;
    }

}
